package com.pinyougou.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbUser;
import com.pinyougou.user.service.UserService;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    /**
     * 增加
     */
    @RequestMapping("/add")
    public Result add(Integer smscode ,@RequestBody TbUser user) {

        if(!userService.checkSmsCode(user.getPhone(),smscode)) {
            return new Result(false,"验证码不正确");
        }

        try {
            userService.addUser(user);
            return new Result(true,"增加成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"增加失败!!!");
        }
    }


    /**
     * 验证码生成及加入消息队列
     * @param phone
     */
    @RequestMapping("/sendCode")
    public Result sendCode(String phone) {
        try {
            userService.createCodeAndSaveQueue(phone);
            return new Result(true,"验证码已发送");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"验证码发送失败");
        }
    }

}
